package org.goyo.main.domain.dao.system.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.goyo.common.exception.DBException;
import org.goyo.common.utils.Pager;
import org.goyo.main.domain.dao.BaseDAO;
import org.goyo.main.domain.dao.system.ISysRoleResourceDao;
import org.goyo.main.domain.entity.system.SysRoleResource;
import org.springframework.stereotype.Repository;

@Repository
public class SysRoleResourceDaoImpl extends BaseDAO implements ISysRoleResourceDao{

	@Override
	public List<SysRoleResource> findByResourceId(String resourceId) {
		String hql = "from SysRoleResource where resourceId=?";
		List<SysRoleResource> list = this.findObjects(hql, null, new Object[]{resourceId});
		return list;
	}

	@Override
	public List<SysRoleResource> findByCond(Map<String, Object> cond,
			Pager pager) throws DBException {
		StringBuffer buf = new StringBuffer("from SysRoleResource where 1=1 ");
		List<Object> params = getCondition(buf, cond);
	    String hql = this.addSort(buf, pager);
		return this.findObjects(hql, pager, params.toArray());
	}
	
	public List<Object> getCondition(StringBuffer buf,Map<String, Object> cond){
		List<Object> params = new ArrayList<Object>();
		
		if(cond.get("roleId") != null && !"".equals(cond.get("roleId"))){
			buf.append(" and roleId=? ");
			params.add(cond.get("roleId"));
		}
		if(cond.get("resourceId") != null && !"".equals(cond.get("resourceId"))){
			buf.append("and resourceId=? ");
			params.add(cond.get("resourceId"));
		}
		return params;
	}

}
